package com.example.microservices.logistics.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.microservices.logistics.service.ShipmentService;
import com.example.microservices.logistics.dto.ShipmentRequest;
import com.example.microservices.logistics.model.Shipment;

@RestController
@RequestMapping("/api/logistics")
@RequiredArgsConstructor
public class ShipmentController {
    
    @Autowired
    private ShipmentService shipmentService;
    
    @PostMapping("/shipments")
    public ResponseEntity<?> createShipment(@RequestBody ShipmentRequest request) {
        try {
            Shipment shipment = new Shipment();
            // 设置所有必要的字段
            shipment.setOrderId(request.getOrderId());
            shipment.setReceiverName(request.getReceiverName());
            shipment.setReceiverPhone(request.getReceiverPhone());
            shipment.setReceiverAddress(request.getReceiverAddress());
            shipment.setCarrierName(request.getCarrierName());
            
            // 可以设置一些默认值
            shipment.setStatus("PENDING");  // 设置初始状态
            
            return ResponseEntity.ok(shipmentService.createShipment(shipment));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
} 